import { useState, useEffect } from 'react'
import { getRoleListApi, getRoleIdToUserListApi } from '@/services/roleApi'

// 角色列表
export const useRoleList = (
  pageNum = 1
): [number, IRoleType.IRole[], (page: number) => void] => {
  const [data, setData] = useState<IRoleType.IRole[]>([])
  const [total, setTotal] = useState(0)

  const loadData = async (page: number) => {
    let ret = await getRoleListApi(page)
    setData(ret.data.roles)
    setTotal(ret.data.total)
  }

  useEffect(() => {
    loadData(pageNum)
  }, [])

  return [total, data, loadData]
}

// 根据角色id返回对应用户记录
export const useRoleIdToUserList = () => {
  const loadData = async (roleid: number, page: number) => {
    let ret = await getRoleIdToUserListApi(roleid, page)
    return ret.data
  }
  return loadData
}
